<?php

namespace Home\Controller;
use Home\Controller\BaseController;
use Think\Log;

/**
 * 
 * BaseController 继承了thinkphp的 Controller
 */
class PersonController extends BaseController{
	
	/**
	 * 显示用户列表
	 */
	public function listPerson(){
		// 临时关闭当前模板的布局功能
		//layout(false);
		echo "dsaf";
		$personModel = M('Person');
		$personList = $personModel->where('id>2')->select();
		
		foreach($personList as $personItemKey=>$personItemObj ){
			//将性别转为配置文件中的字符串
			//$personList[$personItemKey]['sex'] = C("SEX")[$personItemObj["sex"]];
		}

		//进行原生的SQL查询
		//$personList = $personModel->query('SELECT * FROM hms_person');
		//dump($personList);
		
		//将查询出来的数据存放到模板的变量中
		$this->assign("personList",$personList);
		
		//进入Application\Home\View\Person\list.html
		$this->display();		 
	}
	
	/**
	 * 添加用户
	 */
	public function addPerson(){
		echo $_POST["submit"] ;
// 		if($_POST["submit"] == "添加"){}
		//判断是否是Post方式提交
		if(IS_POST){
			//dump($_POST);
			$userInput = array(  
					  'name' =>  '黄彪' ,
					  'password' =>  '123456' ,
					  'birthday' =>  '1988-11-30',
					  'email' =>  'hbiao68@yeah.net',
					  'sex' =>  '2' ,
					  'hobby' =>  '足球,篮球',
					  'submit' =>  '添加');
			// 实例化User模型
			$person = M('Person');
			//将字符串日期转为时间戳
			$userInput['birthday'] =   strtotime($userInput['birthday']);
			//密码使用md5加密
			$userInput['password'] = md5 ($userInput['password']);
			
			$person->data($userInput)->add();
			
			//重定向到list页面
			redirect("/Home/Person/listPerson.html");
		}else{
			//记录日志
			Log::write('进入添加用户界面','ERR');
			$url = U('Home/Person/addPerson');
			//将变量输出到前端，注意，第一个参数是没有"$"符号的
			$this->assign('url',$url);
			$this->display();
		}
		
	}
	
	/**
	 * 添加用户
	 */
	public function updatePerson(){
	    $id = I("get.id");
	    if($id != undefined){
	        //$personModel = M('Person');
	        $personModel = D('Home/Person');
	        $queryCondition["id"] = $id;
	        //查询条件	
	        $personObj = $personModel->where($queryCondition)->select();
	        $personObj[0]["sex"]=2;
	        $this->assign("personObj",$personObj[0]);
	        dump($personObj);
	        //重定向到list页面
	        $this->display();
	    }else{
	        redirect("/Home/Person/listPerson.html");
	    }
		 
	}
	
	/**
	 * 删除信息
	 */
	public function delPerson(){
	    $id = I("get.id");
	    if($id != undefined){
    	    //$personModel = M('Person');
    	    $personModel = D('Home/Person');
    	    $queryCondition["id"] = $id;
    	    
    	    $personModel->where($queryCondition)->delete(); 
    	    //重定向到list页面
    		redirect("/Home/Person/listPerson.html");
	    }else{
	        redirect("/Home/Person/listPerson.html");
	    }
	}
	
}

?>